package model;

import java.util.Queue;
import java.util.LinkedList;
import java.util.Random;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import java.io.FileNotFoundException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.util.List;

public class AcolyteDeck extends AcolyteZone
{
  //private Queue<AcolyteCard> cards;
  
  public AcolyteDeck()
  {
    super(false, null, "deck");
  }
  
  public AcolyteDeck(File deckFile, AcolytePlayer owner)
  {
    super(false, owner, "deck");
    try
    {
      FileInputStream in = new FileInputStream(deckFile);
      XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(in);
      setup(reader);
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
    catch(XMLStreamException e)
    {
      e.printStackTrace();
    }
  }
  
  public AcolyteDeck(File deckFile)
  {
    super(false, null, "deck");
    try
    {
      FileInputStream in = new FileInputStream(deckFile);
      XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(in);
      setup(reader);
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
    catch(XMLStreamException e)
    {
      e.printStackTrace();
    }
  }
  
  public AcolyteDeck(String filename)
  {
    super(false, null, "deck");
    cards = new LinkedList<AcolyteCard>();
    try
    {
      FileInputStream in = new FileInputStream("decks/" + filename);
      XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(in);
      setup(reader);
    }
    catch(IOException e)
    {
      e.printStackTrace();
    }
    catch(XMLStreamException e)
    {
      e.printStackTrace();
    }
  }
  
  public void setup(XMLStreamReader reader) throws XMLStreamException
  {
    while("deck" != reader.getLocalName()) reader.nextTag();
    while(reader.hasNext())
    {
      reader.nextTag();
      if(reader.isStartElement())
      {
        if("card" != reader.getLocalName())
          continue;//skip this tag if it isn't a card tag
        int count;
        AcolyteCard cardToAdd;
        count = Integer.valueOf(reader.getAttributeValue(null, "number"));
        cardToAdd = new AcolyteCard(reader.getAttributeValue(null, "file"));
        
        for(int i=0; i < count; i++)
        {
          cards.add(cardToAdd);
        }
      }//we are done reading the deck
      else if("deck" == reader.getLocalName())
      {
        break;
      }
    }
  }
  
  public void shuffle()
  {
    AcolyteCard[] cardBuffer = cards.toArray(new AcolyteCard[0]);
    //swap with random places in the array
    for(int i=0; i < cardBuffer.length; i++)
    {
      Random rnd = new Random();
      int swap = rnd.nextInt(cardBuffer.length);
      AcolyteCard temp = cardBuffer[i];
      cardBuffer[i] = cardBuffer[swap];
      cardBuffer[swap] = temp;
    }
    cards = new LinkedList<AcolyteCard>();
    //put them back into the queue
    for(int i=0; i < cardBuffer.length; i++)
    {
      cards.add(cardBuffer[i]);
    }
  }
  
  public AcolyteCard topCard()
  {
    return draw();
  }
  
  public AcolyteCard draw()
  {//TODO special message if no cards left
    AcolyteCard toDraw = cards.remove();
    return toDraw;
  }
  
  public Queue<AcolyteCard> getLibrary()
  {
    return cards;
  }
  public String toString()
  {
    String result = "";
    for(AcolyteCard card : cards)
    {
      result +=card.get("name");
      result += "\n";
    }
    return result;
  }
  
  public int count(AcolyteCard toCount)
  {
    int result = 0;
    for(AcolyteCard card : cards)
    {
      if(toCount.get("name").equals(card.get("name")))
      {
        result++;
      }
    }
    return result;
  }
  
  public void save(String filename)
  {
    String toWrite = this.toXML();
    //TODO open a stream and write toWrite to it
    System.out.println(toWrite);
    try
    {
      FileWriter out = new FileWriter("decks/" + filename);
      out.write("toWrite");
    }
    catch(IOException e)
    {
    }
  }
  
  public void save(File fileToWrite)
  {
    String toWrite = this.toXML();
    System.out.println(toWrite);
    try
    {
      FileWriter out = new FileWriter(fileToWrite);
      
      out.write(toWrite);
      out.flush();
      out.close();
    }
    catch(IOException e)
    {
      System.out.println(e);
    }
  }
  
  public String toXML()
  {
    String result = "<deck>\n";
    List<String> names = new LinkedList<String>();
    for(AcolyteCard card : cards)
    {
      //make sure we haven't added this card yet
      if(!names.contains(card.get("name")))
      {
        result += "\t<card name=\"" + card.get("name") + "\" file=\"";
        result += card.get("filename") + "\" number=\"" + count(card);
        result += "\"/>\n";
        //add this to the list of already done card names
        names.add(card.get("name"));
      }
    }
    result += "</deck>\n";
    return result;
  }
}